import request from '@/utils/request'

const state = {
  deviceCount: {
    // 设备在线、离线数量
    offlineCount: 0,
    count: 0,
    onlineCount: 0,
  },
  floors: ['全部'], // 楼层信息
  deviceList: [], // 设备列表
  totalPages: 0, // 设备列表查询结果总数
  currentDeviceType: '传感器',
  loading: true,
  typeCountMap: {}, // 各种类型的设备数量
  currentDevice: {
    name: '公共五合一传感器',
    type: '传感器',
    properties: {
      Temp: 27,
      Humi: 52,
    }
  }, // 当前打开的设备(详情页使用)
}

const mutations = {
  SET_DEVICE_COUNT: (state, data) => {
    state.deviceCount = { ...data, offlineCount: data.count - data.onlineCount }
  },
  UPDATE_FLOORS: (state, data) => {
    state.floors = ['全部', ...data]
  },
  UPDATE_DEVICE_LIST: (
    state,
    data = { isResfresh: false, docs: [], count: 0 }
  ) => {
    // 追加数据列表（分页）
    state.loading = false
    if (data.isResfresh) {
      state.deviceList = data.docs
    } else {
      state.deviceList = [...state.deviceList, ...data.docs]
    }
    state.totalPages = data.count
  },
  CLEAR_DEVICE_LIST: (state) => {
    // 切换设备类型查询的时候清空设备列表
    state.deviceList = []
  },
  SET_DEVICE_TYPE: (state, data) => {
    state.currentDeviceType = data
  },
  UPDATE_LOADING: (state) => {
    state.loading = true
  },
  UPDATE_TYPE_COUNT: (state, data) => {
    state.typeCountMap = data
  },
  UPDATE_CURRENT_DEVICE: (state, data) => {
    console.log('### 设置当前设备: ', data);
    state.currentDevice = data
  }
}

const actions = {
  getDeviceCount({ commit }, projectId) {
    request('/api/device/online/count', { projectId }).then((res) => {
      commit('SET_DEVICE_COUNT', res?.result)
    })
  },
  /**
   * 查询各种设备类型的数量
   * @param {*} 
   * @param {*} projectId 项目ID
   */
  getDeviceTypeCount({ commit }, projectId) {
    request('/api/device/type/count', { projectId }).then((res) => {
      commit('UPDATE_TYPE_COUNT', res?.result)
    })
  },
  fetchFloors({ commit }, data = { type: '传感器', projectId: '' }) {
    request('/api/device/list/roomNumber', data).then((res) => {
      commit('UPDATE_FLOORS', res?.result)
    })
  },
  fetchDeviceList(
    { commit },
    data = {
      projectId: '',
      location: '',
      type: ['传感器'],
      pageNo: 1,
      pageSize: 20,
    }
  ) {
    commit('UPDATE_LOADING')
    request('/api/device/list', data, 'POST')
      .then((res) => {
        let isResfresh = false
        if (data.pageNo === 1) {
          isResfresh = true
        }
        commit('UPDATE_DEVICE_LIST', {
          isResfresh,
          docs: res?.result?.content,
          count: res?.result.totalPages,
        })
      })
      .catch(() => {
        // commit('CLEAR_DEVICE_LIST')
      })
  },
  fetchDeviceDetail({ rootGetters }, data) {
    return request({
      url: '/api/operation/current/data',
      method: 'POST',
      data: { projectId: rootGetters.projectId, ...data, },
    })
  },
  // 设备反控
  async control({ rootGetters }, data) {
    return request({
      url: '/api/operation/reverse/controller',
      method: 'POST',
      data: { ...data, projectId: rootGetters.projectId },
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
